<?php
class Shop_CategoryController extends Promotor_Controller_Action_Scaffold {
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_shop',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		),
		'display' => array(
			'personalized' => true,
			'layout' => 'shop_category_display'
		)
	);

	public $ajaxable = array(
		'list' => array('jsTree'),
	);
	
	public $acl = array(
		'list' => 'list',
		'add' => 'create',
		'edit' => 'update',
		'delete' => 'delete',

		'display' => 'show',
		'move' => 'update'
	);

	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'Shop_Model_Category';
	protected $_formAddClass = 'Shop_Form_Category_Add';
	protected $_formEditClass = 'Shop_Form_Category_Edit';
	protected $_formRemoveClass = 'Shop_Form_Category_Remove';
	protected $_pagination = false;
	// end of specialization

	
	public function init() {
		/* @var Zend_Controller_Action_Helper_AjaxContext */
		$ajaxContext = $this->_helper->getHelper('AjaxContext');
		if (!$ajaxContext->hasContext('jsTree')) {
			$ajaxContext->addContext('jsTree', array(
				'suffix' => 'jstree'
			));
		}
        $ajaxContext->initContext();
	}
	
	/**
	 * @return void
	 */
	public function indexAction() {
		$this->_forward('list');
	}

	/**
	 * Wyświetlanie kategorii i produktów
	 * @return void
	 */
	public function displayAction() {
		$config = $this->_helper->config('config.ini');

		$model = new Shop_Model_Category();

		/* @var $rq Zend_Controller_Request_Http */
		$rq = $this->getRequest();

		$alias = $rq->getParam('alias', $rq->getParam('id'));
		$this->view->alias = $alias;

		$row = null;
		if (strlen($alias) && $alias !== $config->start_category) {
			if (is_numeric($alias)) {
				$row = $model->findByIdCache($alias);
			} elseif (is_string($alias)) {
				$row = $model->findByAliasCache($alias);
			}

			if (null === $row) {
				/* @var $renderer Zend_Controller_Action_Helper_ViewRenderer */
				$renderer = $this->_helper->getHelper('ViewRenderer');
				$renderer->render('display.error');
				return;
			}			
		}

		$this->view->row = $row;
		
			$page   = $rq->getParam('page', 1);
			$onPage = $config->paginator->onPage;

			$model = new Shop_Model_Product();
			$model->findAllInCategory();
			list($rowset, $select) = $model->findAllInCategoryCache($row, $page, $onPage);

		$this->view->rowset = $rowset;

			$paginator = Zend_Paginator::factory($select);
			$paginator->setCurrentPageNumber($page);
			$paginator->setItemCountPerPage($onPage);

		$this->view->paginator = $paginator;
	}
	
	/**
	 * Manipuluje położeniem zagnieżdzeń
	 * @return void
	 */
	public function moveAction() {
		/* @var $model Shop_Model_Category */
		$model = new $this->_modelClass();
		$dbTable = $model->getDbTable();

		$form = new Shop_Form_Category_Move(array('rowset' => $dbTable->fetchAll()));

		$rq = $this->getRequest();
		if (!$rq->isPost()) {
			$this->view->form = $form;
			return;
		}

		if (!$form->isValid($rq->getPost())) {
			$this->view->form = $form;
			return;
		}

		// magic
        $model->move($this->_getParam('id'),
        			 $form->getSubForm('movetab')->getValue('movePk'));

		$status = $model->getStatus();
		$messages = $model->getMessages();

		$flashMessenger = $this->_helper->getHelper('flashMessenger');
		$flashMessenger->addMessage($status);
		foreach ($messages as $message) {
			$flashMessenger->addMessage($message);
		}

		if ($status == Shop_Model_Category::SUCCESS) {
			$this->_helper->redirector->goToAndExit('list');
		} else {
			$this->view->form = $form;
		}
    }
}